'use strict';
const db = uniCloud.database()
const dbCmd = db.command
const setTimeDateFmt = s => {
	return s < 10 ? '0' + s : s;
}

exports.main = async (event, context) => {
	const now = new Date()
	let month = now.getMonth() + 1
	let day = now.getDate()
	let hour = now.getHours()
	let minutes = now.getMinutes()
	let seconds = now.getSeconds()
	month = setTimeDateFmt(month)
	day = setTimeDateFmt(day)
	hour = setTimeDateFmt(hour)
	minutes = setTimeDateFmt(minutes)
	seconds = setTimeDateFmt(seconds)
	let orderCode = now.getFullYear().toString() + month.toString() + day + hour + minutes + seconds + (Math
		.round(Math.random() * 1000000)).toString();



	//event为客户端上传的参数
	const res = await db.collection('orders')
		.add({
			"no": orderCode,
			"type": event.type,
			"status": -1,
			"user_id": event.user_id,
			"paramedic_id": event.paramedic_id,
			"level": event.level,
			"arrangement": event.arrangement,
			"arrage_day": event.arrage_day,
			"arrage_hour": event.arrage_hour,
			"day_sum": event.day_sum,
			"hour_sum": event.hour_sum,
			"start_date": event.start_date,
			"end_date": event.end_date,
			"start_hour": event.start_hour,
			"end_hour": event.end_hour,
			"tel": event.tel,
			"patient_name": event.patient_name,
			"patient_age": event.patient_age,
			"hospital_id": event.hospital ? event.hospital._id : null,
			"ward": event.ward,
			"mark": event.mark,
			"total_price": event.total_price,
			"score": event.score,
			"tags": event.tags,
			"comment": event.comment,
			"pay_type": event.pay_type,
			"service_location": event.service_location,
			"service_id": event.service_id,
			"created_at": new Date().getTime(),
			"updated_at": new Date().getTime(),
			"home_location": event.home_location ? event.home_location : null,
			"plus":event.plus
		})
	console.log(res)


	await db.collection('uni-id-users')
		.where({
			_id: event.paramedic_id
		})
		.update({
			paramedic_profile: {
				arrangement: dbCmd.push(event.arrangement)
			}
		})
	//返回数据给客户端
	return {
		no: orderCode,
	}
};
